// mircoApp/meituan_refund/detail/detail.js
var QQMapWX = require('../../../mymap/qqmap-wx-jssdk.min.js');
const app = getApp();
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: app.globalData.imgPath,
    wechat_app_id:"",

    share_popup: false,
    posterShow: false,
    isFrontCanvas: true, //是否前端控制生成海报，false为后端生成
    posterImg: null,

    share_reward:{},
    order_reward:{},

    detail: {},
    CarouseList: [],
    address: "",

    service_QRcode: "",
    service_mobile: "",
    cservice: "",
    isShowChat: false,

    show_popup: false,

    cityId: "",
    cate_id: "",
    lng: "",
    lat: "",
    dealid: "",

    dealBaseInfo: {},
    couponInfo: {},
    shopInfo: {},
    defaultPic: '',
    share_amount: 0,
    amount: 0,

    group_code: {},
    language: "",
    show1: false,

    city_name: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync('langIndex');
    let address_point = wx.getStorageSync("address-point");
    this.setData({
      'language': language.en,
    });
    if(address_point){
      this.data.lng = address_point.point.lng;
      this.data.lat = address_point.point.lat;
    }
    if(options.dealid){
      this.setData({dealid: options.dealid});
    }
    if (options.path) { //从搜索页/聚合页进入
      let field = ['cate_id', 'lng', 'lat'];
      field.forEach(item => {
        if (item == 'cate_id') {
          let meituanCategory = wx.getStorageSync('meituanCategory');
          this.data[item] = meituanCategory[item];
        }else {
          this.data[item] = options[item];
        }
      })
      this.setData({cityId:options.cityId});
      this.getData();
    } else if (options.city) { //从选择地址页进入
      let dealid = wx.getStorageSync('meituan_dealid');
      let meituanCategory = wx.getStorageSync('meituanCategory');
      this.setData({cityId:options.cityId});
      this.data.city_name = options.city;
      this.data.cate_id = meituanCategory.cate_id;
      this.data.dealid = dealid;
      wx.setStorageSync('meituan_city_id', options.cityId)
    } else if(options.scene){ //扫码进入
      let scene = decodeURIComponent(options.scene);
      // 扫码进入
      if (scene) {
        let info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == 'mid') {
            app._setMid(chil_arr[1]);
          }
          if (chil_arr[0] == 'cate_id') {
            this.data.cate_id = chil_arr[1];
            wx.setStorageSync("meituanCategory", {'cate_id':chil_arr[1]});
          }
          if (chil_arr[0] == 'dealid') {
            this.data.dealid = chil_arr[1];
            wx.setStorageSync("meituan_dealid",chil_arr[1]);
          }
        }
      }
      let address_point = wx.getStorageSync("address-point");
      if (address_point) {
        let city_name = address_point.city
        this.setData({city_name});
        this.data.lng = address_point.point.lng;
        this.data.lat = address_point.point.lat;
        this.get_city_id(city_name);
      }else{
        this.getData();
      }
    }
  
  },
  get_city_id(name) {
    app._postNetWork({
      url:app.getNetAddresss("plugin.meituan-group-buy.frontend.good.city"),
      data:{name},
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result!==1){
          return app.tips(msg);
        }
        let city_id;
        for (let item in data) {
          city_id = data[item][0].id;
        }
        this.setData({city_id})
        wx.setStorageSync('meituan_city_id', city_id)
        this.getData();
      }
    })
  },
  closePop() {
    this.setData({
      posterShow: false
    })
  },
  setPosterShow() {
    this.setData({
      posterShow: true
    })
  },
  goto() {
    wx.navigateTo({
      url: '/packageI/citypickerPage/citypickerPage?plu=meituan&path=detail'
    })
  },
  buyOrder() {
    wx.navigateToMiniProgram({
      appId:this.data.wechat_app_id,
      path:this.data.couponInfo.miniProgramUrl
    })
  },
  showPop() {
    this.setData({
      show1: true
    })
  },
  getData() {
    let json = {
      dealid: this.data.dealid,
      first_cate_id: this.data.cate_id,
      sort: 1,
      geo_info: {
        cityId: "",
        lat: "",
        lng: ""
      }
    }
    if(this.data.lng||this.data.lat){
      json.geo_info.lng=this.data.lng;
      json.geo_info.lat=this.data.lat;
    }
    if(this.data.cityId){
      json.geo_info.cityId=this.data.cityId;
    }

    app._postNetWork({
      url: app.getNetAddresss('plugin.meituan-group-buy.frontend.good.good'),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          console.log(data, "data============")
          let records = data.records[0],
            _cservice = data.good_detail.cservice || {},
            _group_code = data.good_detail.group_code || {};
          let dealBaseInfo = records.dealBaseInfo;
          let couponInfo = records.couponInfo;
          let shopInfo = records.shopInfo;
          let defaultPic = records.dealBaseInfo.defaultPic
          let share_amount = records.dealBaseInfo.share_reward.amount;
          let amount = records.dealBaseInfo.order_reward.amount;
          let service_QRcode = _cservice.service_QRcode;
          let service_mobile = _cservice.service_mobile;
          let cservice = _cservice.cservice;
          let group_code = _group_code;
          this.data.wechat_app_id = data.wechat_app_id;
          this.setData({
            dealBaseInfo,
            couponInfo,
            shopInfo,
            defaultPic,
            share_amount,
            amount,
            service_QRcode,
            service_mobile,
            cservice,
            group_code,
            order_reward:records.dealBaseInfo.order_reward,
            share_reward:records.dealBaseInfo.share_reward,
          })

        } else {
          app.tips(msg);
        }
      }
    })
  },

  gotoHome() {
    wx.navigateTo({
      url: '/mircoApp/meituan_refund/index/index',
    })
  },
  // 群活码跳转
  toGroupCode() {
    if (this.data.group_code.jump_mode === 'home') {
      wx.reLaunch({
        url: '/packageE/groupCode/groupCode',
      });
    } else if (this.data.group_code.code_mini_url) {
      wx.navigateTo({
        url: this.data.group_code.code_mini_url
      });
    }
  },
  goToWXAdress() {

   let point = app.BMapTransqqMap(parseFloat(this.data.lng), parseFloat(this.data.lat));
   if(point){
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      name: this.data.shopInfo.address,
      scale: 18
    });
   }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})